
import 'package:flutter/material.dart';

import 'down.dart';


class _SingleSelectFlow extends StatelessWidget {
  final int itemCount;
  final int selectedIndex;
  final int rowNum;
  final String Function(int index) strGetter;

  _SingleSelectFlow({this.itemCount, this.selectedIndex, this.rowNum, this.strGetter});

  @override
  Widget build(BuildContext context) {

    List<Widget> children = List();

    for(int i=0; i < itemCount; i++){
      children.add(getItemContainer(i,context));
    }

    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.0,vertical: 28.0),
      color: Theme.of(context).colorScheme.surface,
      child: Wrap(
        runSpacing: 12,
        spacing: 12,
        children: children,
      ),
    );
  }

  Widget getItemContainer(int index,BuildContext context) {

    bool isSelect = index == selectedIndex;

    return GestureDetector(
      child: Container(
        width: (MediaQuery.of(context).size.width - 40 - 12 * (rowNum - 1)) / rowNum,
        height: 32.0,
        alignment: Alignment.center,
        child: Text(
          strGetter(index),
          textAlign: TextAlign.center,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
              color: isSelect ? Color(0xFF4974F5) : Color(0xFF222222),
              fontSize: 12.0,
              fontWeight: isSelect ? FontWeight.bold : FontWeight.normal
          ),
        ),
        decoration: BoxDecoration(
            color: isSelect ? Color(0xFFF0F4FF) : Color(0xFFF8F8F8),
            borderRadius: BorderRadius.all(Radius.circular(2))
        ),
      ),
      onTap: (){
        Navigator.pop(context, index);
      },
    );
  }
}


Future<int> cpsShowDropDownSingleFlow({
  @required BuildContext context,
  @required double yAxis,
  @required int itemCount,
  @required int rowNum,
  @required String Function(int index) strGetter,
  int selectedIndex,
}) {
  return cpsShowDropDownDialog<int>(
    context: context,
    builder: (context) {
      return _SingleSelectFlow(
        itemCount: itemCount,
        selectedIndex: selectedIndex,
        rowNum: rowNum,
        strGetter: strGetter,
      );
    },
    yAxis: yAxis,
  );
}